var imgKx = "img/web/kx.gif";
var imgYs = "img/web/ys.gif";
var imgZx = "img/web/zx.gif";

var args = {};
var param = {reqData : args, reqUrl : "querySeat"};

$(function()
{
	if(!json || !json.playId)
	{
		location.href = "login.jsp";
	}
	
	querySeat();
	
	$("#btnReset").click(function()
	{
		querySeat();
		$("#spanSeatInfo").html("");
	});
	
	$("#btnBack").click(function()
	{
		window.location = "time.jsp";
	});
	
	$("#btnChoose").click(nextStep);
});

querySeat = function()
{
	args.playId = json.playId;

	ajaxRequest(param, function(returnData)
	{
		if(returnData.movie)
		{
			var movie = returnData.movie;
			
			var playPrice = movie.playPrice;
			$("#hdPlayPrice").val(playPrice);
			
			var html = movie.movieName + "(" + movie.editionName + ")&nbsp;&gt;&nbsp;" + movie.playTime3 + "&nbsp;" + movie.playTime2;
			$("#spanMovieInfo").html(html);
		}

		createSeat(returnData.seat);
	}, null);
}

createSeat = function(seatedArr)
{
	var dls = [];
	var seatNo = 0;
	
	for(var i=1; i<=7; i++)
	{
		var dl = "<dl><dt>" + i + "</dt>";
		
		for(var j=1; j<=15; j++)
		{
			if(seatNo == 0)
			{
				seatNo = j;
			}
			
			var flag = false;
			
			$(seatedArr).each(function()
			{
				var seatedNo = parseInt(this + "");
				
				if(seatedNo == seatNo)
				{
					flag = true;
					return;
				}
			});
			
			var dd = "<dd><a>";
			
			if(flag)
			{
				dd += "<img src=\"" + imgYs + "\")/>";
			}
			else
			{
				dd += "<img src=\"" + imgKx + "\" onclick=\"chooseSeat(this," + seatNo + ")\"/>";
			}
			
			dd += "</a><br/>" + seatNo + "</dd>";
			dl += dd;
			
			seatNo ++;
		}
		
		dl += "</dl>";
		
		dls.push(dl);
	}
	
	$("#divSeat").html(dls.join(""));
}

chooseSeat = function(obj, seatNo)
{
	var flag = false;
	var seatStr = $("#spanSeatInfo").text();
	var seatArr = seatStr.match(/\d+/g);
	var tempArr = new Array();

	$(seatArr).each(function()
	{
		if(parseInt(this + "") == seatNo)
		{
			flag = true;
			/*
			 * jquery中continue,break的用法
			 * continue:return true;
			 * break:return false;
			 */
		}
		else
		{
			tempArr.push(this);
		}
	});

	if(flag)
	{
		$(obj).attr("src", imgKx);
	}
	else
	{
		$(obj).attr("src", imgZx);
		tempArr.push(seatNo);
	}

	var html = "";
	tempArr.sort(compare);

	$(tempArr).each(function()
	{
		html += this + "号座&nbsp;";
	});

	$("#spanSeatInfo").html(html);
}

compare = function(val1, val2)
{
	return val1 - val2;
}

nextStep = function()
{
	var seatStr = $("#spanSeatInfo").text();
	var seatArr = seatStr.match(/\d+/g);
	
	if(!seatArr)
	{
		alert("请选择座位!");
		return;
	}
	
	if(seatArr.length > 4)
	{
		alert("海峰会员每场次限购4张影票,如您的购票需求大于4张,请您在完成此次购票操作后再进行一次购票操作!");
		return;
	}
	
	var playId = json.playId;
	var memberId = json.memberId;
	var playPrice = $("#hdPlayPrice").val();
	var seat = seatArr.join(",");
	
	var _args = {ticketPlay : playId, ticketMember : memberId, ticketSeat : seat, ticketPrice : playPrice, ticketFlag : 1};
	var _param = {reqData : _args, reqUrl : "addTicket"};
	
	ajaxRequest(_param, function(returnData)
	{
		if(returnData.result)
		{
			alert("购票成功!");
			
			location.reload();
		}
		else
		{
			alert("请检查余额是否足够!");
		}
	}, null);
}